var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { dragElement, sendTextToClipboard, hideEmptyElement, removeEndChar } from "./func.js";
let tocContainer = document.querySelector('.toc');
if (tocContainer) {
    // TOC容器无内容隐藏
    let tocNav = tocContainer.querySelector('nav');
    hideEmptyElement(tocNav, tocContainer);
    // TOC容器拖拽
    dragElement(tocContainer, tocContainer);
}
// 一键复制代码块内容
document.querySelectorAll('.highlight table tr').forEach((tr) => {
    var _a;
    // 获取代码块元素
    let codeElement = tr.querySelector('td:not(:first-of-type)');
    if (!codeElement)
        return;
    // 遍历每一个代码块td，在右上角插入复制按钮
    let copyButton = document.createElement('button');
    copyButton.className = 'copy_code';
    copyButton.innerText = '复制';
    copyButton.style.visibility = 'visible';
    // 将button插入为td的同级元素
    codeElement.after(copyButton);
    // 获取对应的代码块文本(提前获取，防止codeElement变为null)
    let codeText = ((_a = codeElement.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '';
    // 为复制按钮添加点击事件监听器
    copyButton.addEventListener('click', () => __awaiter(void 0, void 0, void 0, function* () {
        try {
            // 发送代码块文本到剪贴板
            yield sendTextToClipboard(codeText);
            // console.log('代码已复制到剪贴板');
            // 提供一个视觉反馈
            copyButton.innerText = '✓';
            copyButton.style.color = '#4caf50';
            setTimeout(() => {
                copyButton.innerText = '复制';
                copyButton.style.color = '';
            }, 1000);
        }
        catch (err) {
            console.error('无法复制代码：', err);
            // 提供一个视觉反馈
            copyButton.innerText = 'x';
            copyButton.style.color = '#f44336';
            setTimeout(() => {
                copyButton.innerText = '复制';
                copyButton.style.color = '';
            }, 1000);
        }
    }));
});
// 行内代码复制按钮
document.querySelectorAll('p').forEach((inlineCode) => {
    // 获取行内元素
    let codeElements = inlineCode.querySelectorAll(':scope > code');
    if (codeElements.length > 0) {
        codeElements.forEach(codeElement => {
            var _a;
            if (!codeElement)
                return;
            // 遍历每一个行内代码元素，在右侧插入复制按钮
            let copyButton = document.createElement('button');
            copyButton.className = 'copy_code';
            copyButton.innerText = '📋';
            // 将button插入为code的子元素
            codeElement.appendChild(copyButton);
            // 鼠标划过行内代码元素时显示复制按钮
            codeElement.addEventListener('mouseenter', () => __awaiter(void 0, void 0, void 0, function* () {
                copyButton.style.visibility = 'visible';
            }));
            codeElement.addEventListener('mouseleave', () => __awaiter(void 0, void 0, void 0, function* () {
                copyButton.style.visibility = '';
            }));
            // 获取对应的行内代码文本(提前获取，防止codeElement变为null)，并对文本进行预处理
            let codeText = removeEndChar(((_a = codeElement.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '', '📋');
            // 为复制按钮添加点击事件监听器
            copyButton.addEventListener('click', () => __awaiter(void 0, void 0, void 0, function* () {
                try {
                    // 发送代码块文本到剪贴板
                    yield sendTextToClipboard(codeText);
                    // console.log('代码已复制到剪贴板');
                    // 提供一个视觉反馈
                    copyButton.innerText = '✓';
                    copyButton.style.color = '#4caf50';
                    copyButton.style.opacity = '1';
                    setTimeout(() => {
                        copyButton.innerText = '📋';
                        copyButton.style.color = '';
                        copyButton.style.opacity = '0.7';
                    }, 1000);
                }
                catch (err) {
                    console.error('无法复制代码：', err);
                    // 提供一个视觉反馈
                    copyButton.innerText = 'x';
                    copyButton.style.color = '#f44336';
                    copyButton.style.opacity = '1';
                    setTimeout(() => {
                        copyButton.innerText = '📋';
                        copyButton.style.color = '';
                        copyButton.style.opacity = '0.7';
                    }, 1000);
                }
            }));
        });
    }
    else {
        return;
    }
});
